package com.avengers.common.bean.enums;

import com.avengers.core.bean.enums.SymbolEnum;
import com.avengers.core.kit.StringKit;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yu.wang
 * @since 7/13/22 9:34 AM
 **/
public enum ContactErrorMsg {
    /**
     * 过户失败
     */
    common("过户失败"),
    /**
     * 黑名单
     * Parameter value policy error: maybe contact org/idCode/idType in blacklist
     */
    blackList("联系人黑名单", "blacklist", "联系人黑名单", "身份证号黑名单", "联系人被加入黑名单"),
    updateProhibited("域名状态包含注册局禁止更新", "the domain has update prohibited status", "域名状态包含注册局禁止更新"),
    sameContact("过户前后联系人相同", "the same with", "新所有人ID和原所有人ID相同", "过户前后联系人相同", "模板未发生改变", "模板信息未发生变化"),
    unAuth("联系人未实名", "contactTemplateRealNamedNotExist"),
    externalLock("合作方锁定", "域名已被锁定"),
    createFail("创建联系人失败"),
    auditing("联系人实名中", "模板处于审核中"),
    authFail("联系人审核失败", "模板审核未通过", "模板未审核通过", "暂未通过白名单", "审核失败", "审核未通过"),
    emailUnAuth("联系人邮箱未验证"),
    authExpire("证件已过期"),
    domainExpire("域名已过期"),
    isTransferOut("域名正在转出"),
    isTransferBack("域名转回中", "域名已转回"),
    notValid("当前联系人不可用", "is not a valid registrant"),
    cityOrProCodeError("省市区号不正确", "省市区号不正确"),
    updateForbidden("当前域名被禁止过户", "当前域名被禁止过户，请联系相关人员"),
    addressValid("无效的联系地址", "publicStreetCn无效"),
    authIdCardError("个人认证信息不存在", "个人认证信息不存在"),
    wait("处理中", "接口维护时段", "无法连接数据库", "域名已在过户中，当前处理失败", "调用超时");

    public final List<String> keywordList;
    public final String comment;
    public final String sysComment;

    ContactErrorMsg(String comment, String... keywords) {
        if (null == keywords) {
            this.keywordList = null;
        } else {
            this.keywordList = Stream.of(keywords).collect(Collectors.toList());
        }
        this.comment = comment;
        this.sysComment = SymbolEnum.asterisk + comment + SymbolEnum.asterisk;
    }

    public static String transferMsg(String msg) {
        ContactErrorMsg contactErrorMsg = of(msg);
        if (null == contactErrorMsg) {
            return null;
        }
        return contactErrorMsg.comment;
    }

    public static ContactErrorMsg of(String msg) {
        if (StringKit.notBlank(msg)) {
            for (ContactErrorMsg o : values()) {
                if (o.comment.equals(msg)) {
                    return o;
                }
                if (null == o.keywordList) {
                    continue;
                }
                if (o.keywordList.parallelStream().anyMatch(keyword -> msg.toLowerCase().contains(keyword.toLowerCase()))) {
                    return o;
                }
            }
        }
        return null;
    }

    public static String showToAdmin(String msg) {
        return SymbolEnum.asterisk + msg + SymbolEnum.asterisk;
    }
}
